(function($){
var local ={
        getItem:function(){
            var item = window.localStorage.getItem("cart");
            if(null == item)
                return JSON.parse("[]");
            else
                return JSON.parse(item);
        },
        setItem:function(data){
            window.localStorage.removeItem("cart");
            if(typeof data == "string")
                window.localStorage.setItem("cart",data);
            else
           window.localStorage.setItem("cart",JSON.stringify(data));
        }
   };
$.extend({cart:{
    put:function(value){
        var items = local.getItem();
        var flag = true;
        for(var i = 0; i < items.length; i++)
        {
            if(value.id == items[i].id)
            {
                items[i].number += value.number;
                flag = false; 
                break;
            }
        }
        if(flag)
        {
        	items[items.length] = value;
        }
        local.setItem(items);
    },
    remove:function(id){
        var items = local.getItem();
        var tempArray ="";
        for(var i = 0; i < items.length;i++)
        {
            if(items[i].id != id)
                tempArray += JSON.stringify(items[i])+",";
        }
        tempArray = "[" + tempArray.substring(0,tempArray.lastIndexOf(","))+"]";
        local.setItem(tempArray);
    },
    totalPrice:function(){
       var items = local.getItem();
       var total = 0;
       for (var i=0; i < items.length; i++)
         total += (items[i].price)*(items[i].number);
       total = total.toFixed(2);
       return total;
    },
    recTotalPrice:function(){
        var items = local.getItem();
        var total = 0;
        for (var i=0; i < items.length; i++)
          total += (items[i].summary)*(items[i].number);
        total = total.toFixed(2);
        return total;
    },
    get:function(id){
        var items = local.getItem();
        for(var i = 0; i < items.length; i++)
        {
            if(items[i].id == id)
                return items[i];
        }
        return null;
    },
    getAll:function(){
        return local.getItem();
    },
    updateNumber:function(id,number){
        var items = local.getItem();
        for (var i=0; i < items.length; i++)
        {
          if(items[i].id == id)
          {
            items[i].number = number;
            break;
          }
        }
        local.setItem(items);
    },
    length:function(){
        return local.getItem().length;
    },
    count:function(){
        var itmes = local.getItem();
        var count = 0;
        for (var i=0; i < itmes.length; i++)
           count += itmes[i].number;
        return count;
    }
    }
    });
})(jQuery);